<?php
	/**
	 * 函数库
	 * @author haifeng
	 * @time   141008
	 */

if(!function_exists('findClassFile')){
	/**
	 * find loading class file 
	 * @param $class class name
	 */
	function findClassFile($class){
		$classFile = strtolower($class).'.class.php';
		$found = false;
		foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) {
	        $fileName = $path.$classFile;
	        if (file_exists($fileName)) {
	            $found = $fileName;
	            break;
	        }
	    }
	    return $found;
	}

}

if(!function_exists('customError')){
	/**
	 * 自定义错误
	 * @param int $code
	 * @param string $msg
	 * @param string $file
	 * @param int $line
	 */
	function customError($code,$msg,$file,$line){
		$errno = $errno & error_reporting();
	    if ($errno == 0) {
	        return false;
	    }
	    if (!defined('E_STRICT')) {
	        define('E_STRICT', 2048);
	    }
	    if (!defined('E_RECOVERABLE_ERROR')) {
	        define('E_RECOVERABLE_ERROR', 4096);
	    }
	    if (!defined('E_DEPRECATED')) {
	        define('E_DEPRECATED', 8192);
	    }
	    if (!defined('E_USER_DEPRECATED')) {
	        define('E_USER_DEPRECATED', 16384);
	    }
	    $errmsg = "";
	    switch($errno){
	        case E_ERROR:
	            $errmsg .= "Error";
	            break;
	        case E_WARNING:
	            $errmsg .= "Warning";
	            break;
	        case E_PARSE:
	            $errmsg .= "Parse Error";
	            break;
	        case E_NOTICE:
	            $errmsg .= "Notice";
	            break;
	        case E_CORE_ERROR:
	            $errmsg .= "Core Error";
	            break;
	        case E_CORE_WARNING:
	            $errmsg .= "Core Warning";
	            break;
	        case E_COMPILE_ERROR:
	            $errmsg .= "Compile Error";
	            break;
	        case E_COMPILE_WARNING:
	            $errmsg .= "Compile Warning";
	            break;
	        case E_USER_ERROR:
	            $errmsg .= "User Error";
	            break;
	        case E_USER_WARNING:
	            $errmsg .= "User Warning";
	            break;
	        case E_USER_NOTICE:
	            $errmsg .= "User Notice";
	            break;
	        case E_STRICT:
	            $errmsg .= "Strict Notice";
	            break;
	        case E_RECOVERABLE_ERROR:
	            $errmsg .= "Recoverable Error";
	            break;
	        case E_DEPRECATED:
	            $errmsg .= "Deprecated functionality";
	            break;
	        case E_USER_DEPRECATED:
	        	$errmsg .="User Deprecated functionality";
	        	break;
	        default:
	            $errmsg .= "Unknown error ($errno)";
	            break;
        }
        $errmsg .= ": {$errstr}  in {$errfile} on line {$errline}";

        if(defined('DEBUG')){
        	throw new Exception($errmsg);
        }else{
        	Logs::log($errmsg,Logs::ERR);
        }
	}
}

if(!function_exists('createDir')){
	/**
     * 查询并创建目录
     * @param string $path
     * @return boolean
     */
	function createDir($path){
		return is_dir(dirname($path)) || mkdir(dirname($path),0777,true);
	}
}

if(!function_exists('array_clear')){
	/**
	 * 清理空键
	 * @param array $arr
	 * @param mixed $key
	 * @return mixed
	 */
	function array_clear(&$arr ,$key = null){
		if(!is_array($arr)){
			return false;
		}
		foreach($arr as $k=>$a){
			if(is_array($a)) array_clear($a,$k);
			if($a!='0' && empty($a)) unset($arr[$k]);
		}
	}
}

if(!function_exists('array_get')){
	/**
	 * 获取某键的值
	 * @param array $arr
	 * @param string $key
	 * @param mixed  $defualt
	 * @return mixed
	 */
	function array_get($arr,$key,$default=null){
		if(is_null($key)) return $arr;
		foreach(explode('.', $key) as $sgment){
			if ( !is_array($arr) or !array_key_exists($sgment, $arr))
			{
				return $default;
			}
			$arr = $arr[$sgment];
		}
		return $arr;
	}
}

if(!function_exists('array_forget')){
	/**
	 * 删除多维键值
	 * @param array $arr
	 * @param string $key
	 * @return void
	 */
	function array_forget(&$arr ,$key){
		$keys = explode('.', $key);
		while (count($keys) > 1)
		{
			$key = array_shift($keys);
			if ( !isset($arr[$key]) or !is_array($arr[$key]))
			{
				return;
			}
			$arr =&$arr[$key];
		}
		unset($arr[array_shift($keys)]);
	}
}

if(!function_exists('array_dels')){
	/**
	 * 批量删除键值
	 * @param array $arr
	 * @param string $key
	 * @return void
	 */
	function array_dels(&$arr,$key){
		$keys = explode(',', $key);
		$count = count($keys);
		for($i=0;$i<$count;$i++){
			unset($arr[array_shift($keys)]);
		}
	}
}
if(!function_exists('array_divide')){
	/**
	 * 把数组按键/值划分成两个数组
	 * @param array $arr
	 * @return array
	 */
	function array_divide($arr){
		return array(array_keys($arr),array_values($arr));
	}
}
if(!function_exists(CreateImage)){
		
	/**
	 * 图片生成
	 * @param 	string	$data			图片内容
	 * @param 	int		$width			宽度
	 * @param 	int		$height			高度
	 * @param 	int		$offset_left	距离左边的偏移量
	 * @param 	int		$offset_top		距离上边的偏移量
	 * @param 	int		$font_splace	字的行距
	 * @param 	array	$bg				图片背景
	 * @param 	array 	$font			文字信息
	 * @imgpath	文件保存位置
	 * @return boolean
	 */
	function CreateImage($data,$width,$height,$offset_left,$offset_top,$font_splace,$bg,$font,$imgpath){
		
		//创建图片，并填充白色背景色
		$img = imagecreatetruecolor($width,$height);
		$color = imagecolorallocatealpha($img,$bg[0],$bg[1],$bg[2],0);
		imagefilledrectangle($img,0,$height,$width,0,$color);
		
		foreach($data as $key=>$val){
			if($key == 0){
				if(!isset($val['offset'])){
					$splace = $offset_top;
					$left = $offset_left;
				}else{
					$left = $offset_left + $val['offset'];
				}
			}elseif($key == 1){ 
				if(!isset($val['offset'])){
					$splace = $font_splace + $font['size']+$offset_top;
					$left = $offset_left;
				}else{
					$left = $offset_left + $val['offset'];
				}
			}else{
				if(!isset($val['offset'])){
					$splace = $splace + $font_splace + $font['size'];
					$left = $offset_left;
				}else{
					$left = $offset_left + $val['offset'];
				}
			}
			if(isset($val['color'])){
				$fontcolor = imagecolorallocatealpha($img,$val['color'][0],$val['color'][1],$val['color'][2],0);
			}else{
				$fontcolor = imagecolorallocatealpha($img,$font['color'][0],$font['color'][1],$font['color'][2],0);
			}
			imagettftext($img,$font['size'],0,$left,$splace,$fontcolor,$font['file'],$val['str']);
		}
		
		//图片输出
		header('Content-Type:image/png');
  		imagepng($img,$imgpath);
  		imagedestroy($img);
	}
}
if(!function_exists('checkImg')){
	/**
	 * 检查文件类型gif,png,jpg
	 * @param	$file
	 */
	function checkImg($file){
		if(!file_exists($file)) return false;
		
		$hd = fopen($file, "rb");
		$data = fread($hd,8);
		if(!$data) return false;
		
		$arr = unpack("H16head", $data);
		$heads = array(
				'47494638'
				,'FFD8FF'
				,'89504E470D0A1A0A'
			);
		foreach($heads as $val){
			if(strpos($arr['head'],$val) === 0){
				return true;
			};
		}
		return false;
	}
}